package com.net.framework.help.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import com.net.framework.help.R;
import com.net.framework.help.utils.LogUtils;

/**
 * Author：xiaobin.zhang
 * Email：154722566@qq.com
 * Date：2017/4/27 15:36
 * Desc：<--功能描述-->
 */
public class LoadMoreListView extends ListView implements AbsListView.OnScrollListener {
    private View footer;// 底部布局
    int totalItemCount;// 总数量
    int lastVisibieItem;// 最后一个可见的item;
    boolean isLoading;// 判断变量
    private boolean isExistData = true;//是否还存在下一页数据（默认true存在下一页数据）
    IloadListener iLoadListener;// 接口变量
    private int currentPage = 1;

    public LoadMoreListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public LoadMoreListView(Context context) {
        super(context);
        initView(context);
    }

    public LoadMoreListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
        // TODO Auto-generated constructor stub
    }

    // listview加载底部布局
    private void initView(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        footer = inflater.inflate(R.layout.layout_footer_more, null);
        // 设置隐藏底部布局
        footer.findViewById(R.id.footer_layout).setVisibility(View.GONE);
        this.addFooterView(footer);
        this.setOnScrollListener(this);
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // TODO Auto-generated method stub
        if (totalItemCount == lastVisibieItem && scrollState == SCROLL_STATE_IDLE) {
            loadMore();
        }
    }

    public void loadMore() {
        if (!isLoading && isExistData) {
            LogUtils.println("加载跟多==============" + currentPage);
            isLoading = true;
            footer.findViewById(R.id.footer_layout).setVisibility(View.VISIBLE);
            currentPage++;
            // 加载更多（获取接口）
            iLoadListener.onLoad(currentPage);
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        // TODO Auto-generated method stub
        this.lastVisibieItem = firstVisibleItem + visibleItemCount;
        this.totalItemCount = totalItemCount;
    }

    public void setInterface(IloadListener iLoadListener) {
        this.iLoadListener = iLoadListener;
    }

    public void setExistData(boolean existData) {
        isExistData = existData;
    }

    public void pageRestore(boolean isFirstpage) {
        if (isFirstpage) {
            this.currentPage = 1;
        }
    }

    // 加载更多数据的回调接口
    public interface IloadListener {
        void onLoad(int currentPage);

    }

    // 加载完成通知隐藏
    public void loadComplete() {
        isLoading = false;
        footer.findViewById(R.id.footer_layout).setVisibility(View.GONE);
    }
}
